package com.ruoyi.web.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.ruoyi.web.model.C2ApaResourceDetail;
import com.ruoyi.web.model.C2CnidResourceDetail;
import com.ruoyi.web.repository.ResourceDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.*;

@Service
public class C2ResourceDetailService {

    @Autowired
    private DruidDataSource druidDataSource;

    /**
     * 获取 `cnid_source_version` 表中的所有字段数据
     *
     * @return 查询结果
     */
    public List<Map<String, Object>> getCnidSourceVersionDetails() {
        List<Map<String, Object>> result = new ArrayList<>();

        // SQL 查询语句
        String sql = "SELECT id, processname, max90, max95, max97, version, date " +
                "FROM cnid_source_detail";

        try (Connection conn = druidDataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 执行查询并处理结果
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Map<String, Object> data = new HashMap<>();
                    data.put("id", rs.getInt("id"));
                    data.put("processName", rs.getString("processname"));
                    data.put("max90", rs.getDouble("max90"));
                    data.put("max95", rs.getDouble("max95"));
                    data.put("max97", rs.getDouble("max97"));
                    data.put("version", rs.getString("version"));
                    data.put("date", rs.getString("date"));
                    result.add(data);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 获取 `apa_source_version` 表中的所有字段数据
     *
     * @return 查询结果
     */
    public List<Map<String, Object>> getApaSourceVersionDetails() {
        List<Map<String, Object>> result = new ArrayList<>();

        // SQL 查询语句
        String sql = "SELECT id, processname, max90, max95, max97, version, date " +
                "FROM apa_source_detail";

        try (Connection conn = druidDataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 执行查询并处理结果
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Map<String, Object> data = new HashMap<>();
                    data.put("id", rs.getInt("id"));
                    data.put("processName", rs.getString("processname"));
                    data.put("max90", rs.getDouble("max90"));
                    data.put("max95", rs.getDouble("max95"));
                    data.put("max97", rs.getDouble("max97"));
                    data.put("version", rs.getString("version"));
                    data.put("date", rs.getString("date"));
                    result.add(data);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }


    @Autowired
    private ResourceDetailRepository resourceDetailRepository;

    // 获取cnid资源详情并排序
    public List<C2CnidResourceDetail> getCnidResourceDetails(String sortBy, String sortOrder) {
        List<C2CnidResourceDetail> resourceDetails = resourceDetailRepository.findCnidDetail();

        // 根据 sortBy 和 sortOrder 对数据进行排序
        if (sortBy != null && sortOrder != null) {
            Comparator<C2CnidResourceDetail> comparator = getCnidComparator(sortBy, sortOrder);
            resourceDetails.sort(comparator);
        }

        return resourceDetails;
    }

    // 根据字段获取排序器
    private Comparator<C2CnidResourceDetail> getCnidComparator(String sortBy, String sortOrder) {
        Comparator<C2CnidResourceDetail> comparator = null;

        switch (sortBy) {
            case "id":
                comparator = Comparator.comparing(C2CnidResourceDetail::getId);
                break;
            case "processName":
                comparator = Comparator.comparing(C2CnidResourceDetail::getProcessName);
                break;
            case "version":
                comparator = Comparator.comparing(C2CnidResourceDetail::getVersion);
                break;
            case "max90":
                comparator = Comparator.comparingDouble(C2CnidResourceDetail::getMax90);
                break;
            case "max95":
                comparator = Comparator.comparingDouble(C2CnidResourceDetail::getMax95);
                break;
            case "max97":
                comparator = Comparator.comparingDouble(C2CnidResourceDetail::getMax97);
                break;
            case "date":
                comparator = Comparator.comparing(C2CnidResourceDetail::getDate);
                break;
            default:
                comparator = Comparator.comparing(C2CnidResourceDetail::getProcessName); // 默认按进程名称排序
                break;
        }

        if ("desc".equals(sortOrder)) {
            comparator = comparator.reversed();
        }

        return comparator;
    }

    // 获取Apa资源详情并排序
    public List<C2ApaResourceDetail> getApaResourceDetails(String sortBy, String sortOrder) {
        List<C2ApaResourceDetail> resourceDetails = resourceDetailRepository.findApaDetail();

        // 根据 sortBy 和 sortOrder 对数据进行排序
        if (sortBy != null && sortOrder != null) {
            Comparator<C2ApaResourceDetail> comparator = getApaComparator(sortBy, sortOrder);
            resourceDetails.sort(comparator);
        }

        return resourceDetails;
    }

    // 根据字段获取排序器
    private Comparator<C2ApaResourceDetail> getApaComparator(String sortBy, String sortOrder) {
        Comparator<C2ApaResourceDetail> comparator = null;

        switch (sortBy) {
            case "id":
                comparator = Comparator.comparing(C2ApaResourceDetail::getId);
                break;
            case "processName":
                comparator = Comparator.comparing(C2ApaResourceDetail::getProcessName);
                break;
            case "version":
                comparator = Comparator.comparing(C2ApaResourceDetail::getVersion);
                break;
            case "max90":
                comparator = Comparator.comparingDouble(C2ApaResourceDetail::getMax90);
                break;
            case "max95":
                comparator = Comparator.comparingDouble(C2ApaResourceDetail::getMax95);
                break;
            case "max97":
                comparator = Comparator.comparingDouble(C2ApaResourceDetail::getMax97);
                break;
            case "date":
                comparator = Comparator.comparing(C2ApaResourceDetail::getDate);
                break;
            default:
                comparator = Comparator.comparing(C2ApaResourceDetail::getProcessName); // 默认按进程名称排序
                break;
        }

        if ("desc".equals(sortOrder)) {
            comparator = comparator.reversed();
        }

        return comparator;
    }
}
